package ut.servlet.member;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
public class CreatePhone extends HttpServlet{
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		// 取得並驗證使用者輸入資訊
		String phone;
		if(request.getParameter("phone") == null){
			return;
		}else{
			// 驗證手機號碼的格式：0 開頭，10 個數字
			phone = request.getParameter("phone");
			if(!phone.matches("0{1}9{1}[0-9]{8}")){
				return;
			}
		}
		String code = Integer.toString((int)(Math.random() * 89998 + 10000));
		if(ut.data.User.createPhone((ut.data.User)request.getSession().getAttribute("user"), phone, code)){
			if(ut.service.ShortMessage.sendVerifyCode(phone, code)){
				// 將驗證碼傳到前端顯示
				response.setHeader("result", "true"); // 寄出簡訊成功
			}else{
				response.setHeader("result", "false"); // 寄出簡訊失敗
			}
		}else{
			response.setHeader("result", "false"); // 寄出簡訊失敗
		}
	}
}